//
//  NSDate+StringWithFormat.m
//
//  Created by Alexey Aleshkov on 12.09.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "NSDate+StringWithFormat.h"


@implementation NSDate (StringWithFormat)

- (NSString *)stringWithFormatForCurrentTimeZone:(NSString *)format
{
	return [self stringWithFormat:format forTimeZone:[[NSCalendar currentCalendar] timeZone]];
}

- (NSString *)stringWithFormat:(NSString *)format forTimeZone:(NSTimeZone *)timeZone
{
	NSString *result;
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = timeZone;
	result = [dateFormatter stringFromDate:self];
	[dateFormatter release];
	return result;
}

- (NSString *)stringWithFormat:(NSString *)format forLocaleId:(NSString *)localeId andTimeZone:(NSTimeZone *)timeZone
{
	NSString *result;
	NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.locale = locale;
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = timeZone;
	result = [dateFormatter stringFromDate:self];
	[dateFormatter release];
	[locale release];
	return result;
}

- (NSString *)stringWithFormat:(NSString *)format forSecondsFromGMT:(NSInteger)seconds
{
	NSString *result;
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:seconds];
	result = [dateFormatter stringFromDate:self];
	[dateFormatter release];
	return result;
}

- (NSString *)stringWithFormat:(NSString *)format forLocaleId:(NSString *)localeId andSecondsFromGMT:(NSInteger)seconds
{
	NSString *result;
	NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.locale = locale;
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:seconds];
	result = [dateFormatter stringFromDate:self];
	[dateFormatter release];
	[locale release];
	return result;
}

+ (NSDate *)dateForCurrentTimeZoneFromString:(NSString *)string withFormat:(NSString *)format
{
	return [NSDate dateFromString:string withFormat:format andTimeZone:[[NSCalendar currentCalendar] timeZone]];
}

+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format andTimeZone:(NSTimeZone *)timeZone
{
	NSDate *result;
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = timeZone;
	result = [dateFormatter dateFromString:string];
	[dateFormatter release];
	return result;
}

+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format forLocaleId:(NSString *)localeId andTimeZone:(NSTimeZone *)timeZone
{
	NSDate *result;
	NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.locale = locale;
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = timeZone;
	result = [dateFormatter dateFromString:string];
	[dateFormatter release];
	[locale release];
	return result;
}

+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format andSecondsFromGMT:(NSInteger)seconds
{
	NSDate *result;
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:seconds];
	result = [dateFormatter dateFromString:string];
	[dateFormatter release];
	return result;
}

+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format forLocaleId:(NSString *)localeId andSecondsFromGMT:(NSInteger)seconds
{
	NSDate *result;
	NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	dateFormatter.locale = locale;
	dateFormatter.dateFormat = format;
	dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:seconds];
	result = [dateFormatter dateFromString:string];
	[dateFormatter release];
	[locale release];
	return result;
}

@end
